/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.fl.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.utils.StringUtils;
import com.jeesite.modules.fl.entity.Enterprise;
import com.jeesite.modules.fl.service.EnterpriseService;
import com.jeesite.modules.sys.utils.UserUtils;
import com.jeesite.modules.test.entity.TestData;
import org.apache.shiro.authz.annotation.RequiresGuest;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hibernate.validator.internal.util.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.fl.entity.Content;
import com.jeesite.modules.fl.service.ContentService;

/**
 * 内容管理Controller
 * @author anthony
 * @version 2019-01-07
 */
@Controller
@RequestMapping(value = "${adminPath}/fl/content")
public class ContentController extends BaseController {

	@Autowired
	private ContentService contentService;
	@Autowired
	private EnterpriseService enterpriseService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Content get(String id, boolean isNewRecord) {
		return contentService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("fl:content:view")
	@RequestMapping(value = {"list", ""})
	public String list(Content content, Model model) {
		model.addAttribute("content", content);
		model.addAttribute("enterpriseList", enterpriseService.findList(new Enterprise()));
		return "modules/fl/contentList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("fl:content:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<Content> listData(Content content, HttpServletRequest request, HttpServletResponse response) {
		Page<Content> page = contentService.findPage(new Page<Content>(request, response), content); 
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("fl:content:view")
	@RequestMapping(value = "form")
	public String form(Content content, Model model) {
		model.addAttribute("content", content);
		model.addAttribute("enterpriseList", enterpriseService.findList(new Enterprise()));
		return "modules/fl/contentForm";
	}

	@RequiresPermissions("fl:content:view")
	@RequestMapping(value = "view")
	public String view(Content content, Model model) {
		if(!StringHelper.isNullOrEmptyString(content.getId())) {
			content.setCreateByName(UserUtils.get(content.getCreateBy()).getUserName());
		}
		model.addAttribute("content", content);
		return "modules/fl/contentView";
	}

	/**
	 * 保存内容管理
	 */
	@RequiresPermissions("fl:content:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Content content) {
		contentService.save(content);
		return renderResult(Global.TRUE, text("保存内容管理成功！"));
	}
	
	/**
	 * 删除内容管理
	 */
	@RequiresPermissions("fl:content:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(Content content) {
		contentService.delete(content);
		return renderResult(Global.TRUE, text("删除内容管理成功！"));
	}

	/**
	 * 停用数据
	 */
	@RequiresPermissions("fl:content:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(Content content) {
		content.setStatus(Content.STATUS_DISABLE);
		contentService.updateStatus(content);
		return renderResult(Global.TRUE, text("停用成功"));
	}

	/**
	 * 启用数据
	 */
	@RequiresPermissions("fl:content:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(Content content) {
		content.setStatus(Content.STATUS_NORMAL);
		contentService.updateStatus(content);
		return renderResult(Global.TRUE, text("启用成功"));
	}
}